#!/bin/sh

# THIS VERSION IS BUGGED - IT NEEDS FIXES TO RUN, SORRY #

# Release - 03/07 #

############################ SupTime Project v0.3a ###############################
# Hi, this is really-easy bash script which generates png image with your system #
# top and current uptime.                                                        #
##################################################################################
# Please consider that it is an early alpha version of script which I'm writing  #
# _FOR_PERSONAL_USE_ - so don't wait anything great. Also my linux knowledge is  #
# ather poor, so it is sure that there are many things in this script which      #
# could be improved...                                                           #
##################################################################################
# Script requires:                                                               #
# - ImageMagick >= 6.2.5                                                         #
# - uptimed                                                                      #
# - uprecords (uprecords-cgi)                                                    #
# - ncftp                                                                        #
##################################################################################
# Contact? >> sufixx[at]gmail.com                                                #
##################################################################################


# Configurable variables
BASE_DIR=`pwd` # of course - put here normal path!
OUT=$BASE_DIR/out.png
THEME=theme_alpha

# ftp and ssh settings
UPLOAD="none"	# Choose method of image upload: ftp/scp/none
USER="user"
PASSWORD="password"
HOST="server.com"
FTP_PORT=21
SSH_PORT=22
FTP_DIR="public_html/"
SSH_DIR=""


# No need to change
VERSION="0.3a"
EXTRAFX=1
TMP_DIR=/tmp
THEME_DIR=$BASE_DIR/themes
BACKGROUND=$THEME_DIR/$THEME/bg.png
FOREGROUND=$THEME_DIR/$THEME/fg.png

# Script command-line options
if [ "$1" == "-h" ]; then
	echo "Available options are:"
	echo " -h	Show this help"
	echo " -i	Print information about directories"
	echo " -n	Turn off software checks"
	echo " -x	Turn off extra layer effects"
	echo " -v	Print script version"
	exit;
elif [ "$1" == "-n" ]; then
	CHECKS=0
	echo " [!] Running without software checks"
elif [ "$1" == "-x" ]; then
	EXTRAFX=0
	echo " [+] Running without extra layer effects"
elif [ "$1" == "-v" ]; then
	echo " [i] SupTime script $VERSION"
	exit;
elif [ "$1" == "-i" ]; then
	echo " Information about set directories:"
	echo "Temponary directory:	$TMP_DIR"
	echo "Base directory:		$BASE_DIR"
	echo "Output file location:	$OUT"
	echo "Using theme:		$THEME"
	exit;
fi

# Software checks.
if [ "$CHECKS" != "0" ]; then
	if [ -e /usr/bin/uprecords ]; then		# Check if uprecords is installed
		cat /dev/null
	else
		echo " [-] Please install uprecords program."
		exit;
	fi

	IMAGEMAGICK=`identify --version |head -n 1| awk {'print $3'}| sed 's/\.//' | sed 's/\.//'`

	if [ $IMAGEMAGICK -ge 625 ]; then		# Check if ImageMagick is installed
		cat /dev/null
	else
		echo " [-] Your ImageMagick version is older then 6.2.5"
		exit;
	fi

else
	cat /dev/null
fi

# Read uptime variable from uprecords
UPTIME=`uprecords -a | grep "\->" | awk '{print $3 $4 $5 }' | sed 's/\days/ days/' |sed 's/\,/ /'`

# Read top uptime variable from uprecords
TOP=`uprecords | head -n 3 | tail -n 1 | awk '{print $2 $3 $4}' | sed 's/\days/ days/' |sed 's/\,/ /'`

# Check if theme config exists
if [ -e $THEME_DIR/$THEME/theme.conf ]; then
	THEME_LINES=`cat $THEME_DIR/$THEME/theme.conf|wc -l`
	if [ "$THEME_LINES" -ge 5 ]; then
		cat $THEME_DIR/$THEME/theme.conf | head -n 4 > $TMP_DIR/theme_base.tmp
		source $TMP_DIR/theme_base.tmp

		if [ "$FONTS" == "1" ]; then
			cat $THEME_DIR/$THEME/theme.conf | grep "#FONT1#" | sed 's/\#FONT1# //' > $TMP_DIR/theme1.tmp
			THEME1_LINES=`cat $TMP_DIR/theme1.tmp| wc -l`
			if [ "$THEME1_LINES" -ge 2 ]; then
				source $TMP_DIR/theme1.tmp
			else
				echo " [-] Error in config file!"
				exit;
			fi
		elif [ "$FONTS" == "2" ]; then
			cat $THEME_DIR/$THEME/theme.conf | grep "#FONT1#" | sed 's/\#FONT1# //' > $TMP_DIR/theme1.tmp
			cat $THEME_DIR/$THEME/theme.conf | grep "#FONT2#" | sed 's/\#FONT2# //' > $TMP_DIR/theme2.tmp
			THEME1_LINES=`cat $TMP_DIR/theme1.tmp| wc -l`
			if [ "$THEME1_LINES" -ge 2 ]; then
				source $TMP_DIR/theme1.tmp
			else
				echo " [-] Error in config file!"
				exit;
			fi
			THEME2_LINES=`cat $TMP_DIR/theme2.tmp| wc -l`
			if [ "$THEME2_LINES" -ge 2 ]; then
				source $TMP_DIR/theme2.tmp
			else
				echo " [-] Error in config file!"
				exit;
			fi
		elif [ "$FONTS" == "3" ]; then
			cat $THEME_DIR/$THEME/theme.conf | grep "#FONT1#" | sed 's/\#FONT1# //' > $TMP_DIR/theme1.tmp
			cat $THEME_DIR/$THEME/theme.conf | grep "#FONT2#" | sed 's/\#FONT2# //' > $TMP_DIR/theme2.tmp
			cat $THEME_DIR/$THEME/theme.conf | grep "#FONT3#" | sed 's/\#FONT3# //' > $TMP_DIR/theme3.tmp
			THEME1_LINES=`cat $TMP_DIR/theme1.tmp| wc -l`
			if [ "$THEME1_LINES" -ge 2 ]; then
				source $TMP_DIR/theme1.tmp
			else
				echo " [-] Error in config file!"
				exit;
			fi
			THEME2_LINES=`cat $TMP_DIR/theme2.tmp| wc -l`
			if [ "$THEME2_LINES" -ge 2 ]; then
				source $TMP_DIR/theme2.tmp
			else
				echo " [-] Error in config file!"
				exit;
			fi
			THEME3_LINES=`cat $TMP_DIR/theme3.tmp| wc -l`
			if [ "$THEME3_LINES" -ge 2 ]; then
				source $TMP_DIR/theme3.tmp
			else
				echo " [-] Error in config file!"
				exit;
			fi
			
		fi
	else
		echo " [-] Theme config file is suspiciously short. Check it."
		exit;
	fi
else
	echo " [-] Theme not found! Check if it really exists."
	exit;
fi

# Load text layout of chosen theme
if [ "$LAYOUTS" == "1" ]; then
	cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT1#" | sed 's/\#LAYOUT1# //' > $TMP_DIR/layout1.tmp
	LAYOUT1_LINES=`cat $TMP_DIR/layout1.tmp| wc -l`
	if [ "$LAYOUT1_LINES" -ge 2 ]; then
		source $TMP_DIR/layout1.tmp
	else
		echo " [-] Error in config file!"
		exit;
	fi
elif [ "$LAYOUTS" == "2" ]; then
	cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT1#" | sed 's/\#LAYOUT1# //' > $TMP_DIR/layout1.tmp
	cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT2#" | sed 's/\#LAYOUT2# //' > $TMP_DIR/layout2.tmp
	LAYOUT1_LINES=`cat $TMP_DIR/layout1.tmp| wc -l`
	if [ "$LAYOUT1_LINES" -ge 2 ]; then
		source $TMP_DIR/layout1.tmp
	else
		echo " [-] Error in config file!"
		exit;
	fi
	LAYOUT2_LINES=`cat $TMP_DIR/layout2.tmp| wc -l`
	if [ "$LAYOUT2_LINES" -ge 2 ]; then
		source $TMP_DIR/layout2.tmp
	else
		echo " [-] Error in config file!"
		exit;
	fi
elif [ "$LAYOUTS" == "3" ]; then
	cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT1#" | sed 's/\#LAYOUT1# //' > $TMP_DIR/layout1.tmp
	cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT2#" | sed 's/\#LAYOUT2# //' > $TMP_DIR/layout2.tmp
	cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT3#" | sed 's/\#LAYOUT3# //' > $TMP_DIR/layout3.tmp
	LAYOUT1_LINES=`cat $TMP_DIR/layout1.tmp| wc -l`
	if [ "$LAYOUT1_LINES" -ge 2 ]; then
		source $TMP_DIR/layout1.tmp
	else
		echo " [-] Error in config file!"
		exit;
	fi
	LAYOUT2_LINES=`cat $TMP_DIR/layout2.tmp| wc -l`
	if [ "$LAYOUT2_LINES" -ge 2 ]; then
		source $TMP_DIR/layout2.tmp
	else
		echo " [-] Error in config file!"
		exit;
	fi
	LAYOUT3_LINES=`cat $TMP_DIR/layout3.tmp| wc -l`
	if [ "$LAYOUT3_LINES" -ge 2 ]; then
		source $TMP_DIR/layout3.tmp
	else
		echo " [-] Error in config file!"
		exit;
	fi
fi

# Render image with text from text file
cat $TMP_DIR/up.txt | convert -background transparent -pointsize $FONT_SIZE -fill $FONT_COLOR -font $FONT label:@- -append $TMP_DIR/text.png

# Merge graphical background, text image and foreground 
composite $TMP_DIR/text.png -compose Over $BACKGROUND $TMP_DIR/compisite.png

# Load extra effects files
if [ "$EXTRAFX" == "1" ]; then
	if [ "$EXTRAS" == "1" ]; then
		cat $THEME_DIR/$THEME/theme.conf | grep "#EXTRA1#" | sed 's/\#EXTRA1# //' > $TMP_DIR/extra1.tmp
		EXTRA1_LINES=`cat $TMP_DIR/extra1.tmp| wc -l`
		if [ "$EXTRA1_LINES" -ge 1 ]; then
			source $TMP_DIR/extra1.tmp
			echo " [+] Adding 1 extra effect..."
		else
			echo " [-] Error in config file!"
			exit;
		fi
	elif [ "$EXTRAS" == "2" ]; then
		cat $THEME_DIR/$THEME/theme.conf | grep "#EXTRA1#" | sed 's/\#EXTRA1# //' > $TMP_DIR/extra1.tmp
		cat $THEME_DIR/$THEME/theme.conf | grep "#EXTRA2#" | sed 's/\#EXTRA2# //' > $TMP_DIR/extra2.tmp
		EXTRA1_LINES=`cat $TMP_DIR/extra1.tmp| wc -l`
		if [ "$EXTRA1_LINES" -ge 1 ]; then
			source $TMP_DIR/extra1.tmp
			echo " [+] Adding 1 extra effect..."
		else
			echo " [-] Error in config file!"
			exit;
		fi
		EXTRA2_LINES=`cat $TMP_DIR/extra2.tmp| wc -l`
		if [ "$EXTRA2_LINES" -ge 1 ]; then
			source $TMP_DIR/extra2.tmp
			echo " [+] Adding 2 extra effects..."
		else
			echo " [-] Error in config file!"
			exit;
		fi
	elif [ "$EXTRAS" == "3" ]; then
		cat $THEME_DIR/$THEME/theme.conf | grep "#EXTRA1#" | sed 's/\#EXTRA1# //' > $TMP_DIR/extra1.tmp
		cat $THEME_DIR/$THEME/theme.conf | grep "#EXTRA2#" | sed 's/\#EXTRA2# //' > $TMP_DIR/extra2.tmp
		cat $THEME_DIR/$THEME/theme.conf | grep "#EXTRA2#" | sed 's/\#EXTRA2# //' > $TMP_DIR/extra3.tmp
		EXTRA1_LINES=`cat $TMP_DIR/extra1.tmp| wc -l`
		if [ "$EXTRA1_LINES" -ge 1 ]; then
			source $TMP_DIR/extra1.tmp
			echo " [+] Adding 1 extra effect..."
		else
			echo " [-] Error in config file!"
			exit;
		fi
		EXTRA2_LINES=`cat $TMP_DIR/extra2.tmp| wc -l`
		if [ "$EXTRA2_LINES" -ge 1 ]; then
			source $TMP_DIR/extra2.tmp
			echo " [+] Adding 2 extra effects..."
		else
			echo " [-] Error in config file!"
			exit;
		fi
		EXTRA3_LINES=`cat $TMP_DIR/extra3.tmp| wc -l`
		if [ "$EXTRA3_LINES" -ge 1 ]; then
			source $TMP_DIR/extra3.tmp
			echo " [+] Adding 3 extra effects..."
		else
			echo " [-] Error in config file!"
			exit;
		fi
	fi
else
	cat /dev/null
fi
composite $FOREGROUND -compose Over $TMP_DIR/compisite.png $BASE_DIR/out.png

# Show size of generated image
KBYTES=`du -k $OUT | awk {'print $1'}`
echo " [i] Rendered image size is $KBYTES kB"

# Cleanup
rm -f $TMP_DIR/compisite.png $TMP_DIR/up.txt $TMP_DIR/text.png $TMP_DIR/theme*.tmp $TMP_DIR/extra*.tmp $TMP_DIR/layout*.tmp

# Upload image
if [ "$UPLOAD" == "ftp" ]; then
	ncftpput -u $USER -p $PASSWORD -P $FTP_PORT $HOST $FTP_DIR $OUT > /dev/null
	echo " [i] Image uploaded to ftp server"
elif [ "$UPLOAD" == "scp" ]; then
	echo " [!] Upload via scp not implemented yet."
fi

exit;
